import 'package:flutter/cupertino.dart';
import 'package:yw_bapp/model/bank_list_model.dart';
import 'package:yw_bapp/model/check_bank_model.dart';
import 'package:yw_bapp/model/check_merchant_model.dart';
import 'package:yw_bapp/model/index_data_model.dart';
import 'dart:async';
import 'dart:convert';
import 'package:yw_bapp/model/phone_code_model.dart';
import 'package:http/http.dart' as http;
import 'package:yw_bapp/model/phone_login_model.dart';
import 'package:yw_bapp/model/qiniu_token_model.dart';
import 'package:yw_bapp/model/store_detail_model.dart';
import 'package:yw_bapp/model/store_list_model.dart';
import 'package:yw_bapp/utils/loacl_storage.dart';

const BASER_URL = "https://ywapidev.lemobj.com/api/";
var headerMap = new Map<String, String>.from(
    { "content-type": "application/json", "X-YOUWEI-PLAT": 'wx_b_user' });
// 获取验证码地址
const getCodeURL = BASER_URL + "manager/common/get_code";

// 登录地址
const toLoginURL = BASER_URL + 'manager/common/login';

// 获取门店列表地址
const getStoreListURL = BASER_URL + 'manager/common/get_store_list';

// 检查商户是否存在地址
const checkMerchantURL = BASER_URL + 'manager/common/check_merchant';

// 获取B端首页数据地址
const getBIndexDataURL = BASER_URL + 'manager/b/index';

// 获取门店详情数据地址
const getStoreDetailURL = BASER_URL + 'store/info/';

// 获取银行卡列表地址
const getBankListURL = BASER_URL + 'manager/common/get_bank_list';

// 查询银行卡是否合法
const checkBankURL = 'https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8';

// 获取七牛云的Token
const qiniuToken = 'https://youweiapi.lemobj.com/api/oss_token';

// 公共的
class CommonDao {
// 获取验证码接口
  static Future<PhoneCodeModel> GetCode(String phoneNum) async {
    var params = {"phoneNum": phoneNum};
    final response = await http.post(getCodeURL,
        headers: headerMap, body: jsonEncode(params));
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return PhoneCodeModel.fromJson(result);
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

// 登录或注册
  static Future<PhoneLoginModel> ToLogin(
      String phoneNum, String verifyCode) async {
    var params = {"phoneNum": phoneNum, "verifyCode": verifyCode};
    final response = await http.post(toLoginURL,
        headers: headerMap, body: jsonEncode(params));
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return PhoneLoginModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

// 获取门店列表
  static Future<StoreListModel> GetStoreList() async {
    var token = await getStorage('Token');
    headerMap['Token'] = token.toString();
    final response = await http.get(
      getStoreListURL,
      headers: headerMap,
    );
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return StoreListModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  // 检查商户是否存在
  static Future<CheckMerchantModel> CheckMerchant(merchantCode) async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    headerMap['Token'] = token.toString();
    headerMap['Store-ID'] = storeID.toString();
    var params = {"code": merchantCode};
    final response = await http.post(checkMerchantURL,
        headers: headerMap, body: jsonEncode(params));
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return CheckMerchantModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

// 获取B端首页数据
  static Future<IndexDataModel> GetIndexData() async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    final response = await http.get(
      getBIndexDataURL,
      headers: headerMap,
    );
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return IndexDataModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

// 获取门店详情的数据
  static Future<StoreDetailModel> GetStoreDetail(int storeId) async {
    // var storeID = await getStorage('Store-ID');
    // var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    final response = await http.get(
      getStoreDetailURL + '$storeId',
      headers: headerMap,
    );
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return StoreDetailModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  // 获取银行卡列表
  static Future<BanksListModel> GetBankList() async {
    final response = await http.get(
      getBankListURL,
      headers: headerMap,
    );
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return BanksListModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  // 检查银行卡是否合法
  static Future<CheckBankModel> CheckBank(String bankAccount) async {
    var url = checkBankURL + '&cardNo=${bankAccount}&cardBinCheck=true';
    print(url);
    final response = await http.get(
      url,
      headers: headerMap,
    );
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return CheckBankModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

   // 获取七牛云Token
  static Future<QiniuToken> GetQiniuToken() async {
    final response = await http.get(
      qiniuToken,
      headers: headerMap,
    );
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return QiniuToken.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }
}
